package lab03.test01;
import java.io.*;

public class Test01 {
    public static void main(String[] args) {
        String path = "F:\\hello.txt";
        String line = " ";
        BufferedReader reader = null;
        BufferedWriter writer = null;//初始化
        try {
            writer = new BufferedWriter(new FileWriter(path));
            writer.write("小林");
            writer.newLine();

            writer.write("djksfj");
            writer.newLine();

        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                writer.close();//录入完后，关闭文件并保存
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        try {//读取多行
            reader = new BufferedReader(new FileReader(path));

            while (true) {
                line = reader.readLine();//每次读取一行，因为是在循环语句里面，所以会自动换行
                //直到为null值
                if (line != null) {
                    System.out.println(line);

                }
                else
                    break;
            }

        }
        catch (IOException e) {
            e.printStackTrace();
        }

 //只读一行
    try {
        reader = new BufferedReader(new FileReader(path));
       //因为是if语句，不是循环语句，所以不会换行读取
        if ((line=reader.readLine())!=null) {
            System.out.println(line);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }

    }
}





